using System;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace Sedge.Core.FileSystemWrap
{
	class XmlFileSerializer : IXmlFileSerializer
	{
		public object Load(string path, Type type)
		{
			object result;
			try
			{
				using (FileStream fs = new FileStream(path, FileMode.Open))
				{
					XmlSerializer xs = new XmlSerializer(type);
					result = xs.Deserialize(fs);
				}
			}
			catch
			{
				result = null;
			}
			return result;
		}

		public bool Save<T>(string path, T obj)
		{
			bool result = true;
			try
			{
				using (FileStream fs = new FileStream(path, FileMode.Create))
				{
					XmlSerializer xs = new XmlSerializer(typeof(T));
					using (XmlTextWriter writer = new XmlTextWriter(fs, Encoding.Unicode))
					{
						writer.Formatting = Formatting.Indented;
						xs.Serialize(writer, obj);
					}
				}
			}
			catch
			{
				result = false;
			}

			return result;
		}
	}
}